//
//  AppBarChartView.swift
//  XWHAOfflineFitting
//
//  Created by edy on 2025/9/12.
//

import UIKit
import Charts

class AppBarChartView: UIView {
    
    private(set) lazy var chartView = BarChartView()
    
    lazy var markerView: XWHAHearingTestMarker = XWHAHearingTestMarker(color: UIColor.white, font: AppFont.harmonyOSSans(ofSize: 10, weight: .medium), textColor: majorTextColor, insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8))

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubViews()
        relayoutSubViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func addSubViews() {
        addSubview(chartView)
        
        configChartViewCommon()
        configXAxis()
        configYAxis()
        configMarkerView()
    }
    
    func relayoutSubViews() {
       
   }
    
}

extension AppBarChartView {
    
    @objc func configChartViewCommon() {
        chartView.backgroundColor = .clear
        
        chartView.delegate = self
        chartView.noDataText = ""
        chartView.legend.enabled = false
        
        chartView.scaleXEnabled = false
        chartView.scaleYEnabled = false
        chartView.doubleTapToZoomEnabled = false
        
        // 拖动效果高亮
        chartView.dragEnabled = true
        
        chartView.gridBackgroundColor = .clear
        
        //        chartView.minOffset = 12
        chartView.extraTopOffset = 0
    }
    
    @objc func configXAxis() {
        chartView.xAxis.labelPosition = .bottom
        
        let xMaxValue = 9
        chartView.xAxis.axisMaxLabels = xMaxValue + 1
        
        chartView.xAxis.axisMinimum = 0
        chartView.xAxis.granularity = 1
        chartView.xAxis.axisMaximum = Double(xMaxValue)
        
        //        chartView.xAxis.labelCount = 7
        //        chartView.xAxis.forceLabelsEnabled = true
        //        chartView.xAxis.granularityEnabled = true
        
        chartView.xAxis.axisLineWidth = 1
        chartView.xAxis.axisLineColor = UIColor.black.withAlphaComponent(0.05)
        chartView.xAxis.drawAxisLineEnabled = true
        
        chartView.xAxis.gridLineWidth = 1
        chartView.xAxis.gridColor = UIColor.black.withAlphaComponent(0.05)
        chartView.xAxis.drawGridLinesEnabled = true
        
        chartView.xAxis.labelFont = AppFont.harmonyOSSans(ofSize: 10)
        chartView.xAxis.labelTextColor = minorTextColor
//        chartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: { [weak self] value, _ in
//            let x = value.int
//            var ret = x.string
//            guard let self = self else {
//                return ret
//            }
//            if x < self.freqBandList.count, x >= 0 {
//                let tBand = self.freqBandList[x]
//                if tBand >= 1000 {
//                    ret = "\(tBand / 1000)k"
//                } else {
//                    ret = tBand.string
//                }
//            }
//            return ret
//        })
    }
    
    @objc func configYAxis() {
        //        chartView.leftAxis.enabled = false
        
        let axisMaximum: Double = 120
        let labelCount = 13
        let granularity: Double = 10
        chartView.leftAxis.inverted = true
        chartView.leftAxis.axisMinimum = 0
        chartView.leftAxis.axisMaximum = axisMaximum
        chartView.leftAxis.granularity = granularity
        chartView.leftAxis.setLabelCount(labelCount, force: true)
        
        chartView.rightAxis.inverted = true
        chartView.rightAxis.axisMinimum = 0
        chartView.rightAxis.axisMaximum = axisMaximum
        chartView.rightAxis.granularity = granularity
        chartView.rightAxis.setLabelCount(labelCount, force: true)
        
        let lineColor = UIColor.black.withAlphaComponent(0.05)
        let lineWidth = 0.5
        chartView.leftAxis.axisLineColor = lineColor
        chartView.leftAxis.axisLineWidth = lineWidth
        
        chartView.rightAxis.axisLineColor = lineColor
        chartView.rightAxis.axisLineWidth = lineWidth
        
        chartView.leftAxis.drawGridLinesEnabled = true
        chartView.leftAxis.drawLabelsEnabled = true
        
        chartView.rightAxis.drawGridLinesEnabled = false
        chartView.rightAxis.drawLabelsEnabled = false
        
        chartView.leftAxis.axisLineColor = lineColor
        chartView.leftAxis.axisLineWidth = lineWidth
        
        chartView.leftAxis.gridLineWidth = lineWidth
        chartView.leftAxis.gridColor = lineColor
        
        chartView.rightAxis.gridLineWidth = lineWidth
        chartView.rightAxis.gridColor = lineColor
        
        chartView.leftAxis.labelFont = AppFont.harmonyOSSans(ofSize: 10)
        chartView.leftAxis.labelTextColor = minorTextColor
        
        chartView.rightAxis.labelFont = AppFont.harmonyOSSans(ofSize: 10)
        chartView.rightAxis.labelTextColor = minorTextColor
        
        chartView.leftAxis.valueFormatter = DefaultAxisValueFormatter(block: { [weak self] value, _ in
            let y = value.int / 10
            var ret = ""
            guard let _ = self else {
                return ret
            }
            
            if y % 2 == 0 {
                ret = value.int.string
            } else {
                ret = ""
            }
            
            return ret
        })
    }
    
    @objc func configMarkerView() {
        //        chartView.highlightFullBarEnabled = true
        //        markerView.frame = CGRect(x: 0, y: 0, width: 160, height: 120)
        //        markerView.backgroundColor = .clear
        //        markerView.chartView = chartView
        //        chartView.marker = markerView
        
        markerView.chartView = chartView
        markerView.minimumSize = CGSize(width: 80, height: 40)
        chartView.marker = markerView
    }
    
}

// MARK: - ChartViewDelegate
extension AppBarChartView: ChartViewDelegate {
    
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
//        guard let _ = chartView.data?.dataSets[highlight.dataSetIndex] else { return }
//
//        //        markerView.setShowOffset(chartView, entry: entry, highlight: highlight)
//        ////        let entryIndex = dataSet.entryIndex(entry: entry)
//        //        markerView.textLb.text =  .string
//        //        markerView.detailLb.text = entry.y.int.string
//
//        let x = entry.x.int
//        let y = entry.y.int
//
//        var ret = x.string
//
//        if x < freqBandList.count, x >= 0 {
//            let tBand = freqBandList[x]
//            if tBand >= 1000 {
//                ret = "\(tBand / 1000)k"
//            } else {
//                ret = tBand.string
//            }
//        }
//
//        markerView.setLabel("\(ret)Hz/\(y.string)\(volumeUnitText)")
    }
    
}
